package com.mikao.common;

import org.springframework.core.convert.converter.Converter;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormater implements Converter<String,Date> {

    private  SimpleDateFormat simpleDateFormat ;


    @Override
    public Date convert(String str) {
        if(str == null || "".equals(str) ){
            return null;
        }

        if(str.indexOf(":") == -1){
            str += " 00:00:00";
        }
        if(str.indexOf("/") != -1 ){
            simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        }else if(str.indexOf("-") != -1){
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        Date date = null;
        try {
            date = simpleDateFormat.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }
}
